home *** CD-ROM | disk | FTP | other *** search
/ Creative Computers / Creative Computers CD-ROM, Volume 1 (Legendary Design Technologies, Inc.)(1994).iso / shareware / telecom / term_4.1 / extras / hydracom / hydracom-source.lha / amiga.c < prev    next >
C/C++ Source or Header  |  1994-07-02  |  42KB  |  1,865 lines

  1. /*
  2. **    Amiga support module for HYDRA protocol sample implementation.
  3. **
  4. **    Written by    Olaf Barthel
  5. **            Brabeckstrasse 35
  6. **            D-30559 Hannover
  7. **
  8. **            eMail: olsen@sourcery.han.de
  9. **
  10. **    Freely distributable.
  11. */
  12.  
  13.     /* System includes. */
  14.  
  15. #include <intuition/intuitionbase.h>
  16.  
  17. #include <libraries/gadtools.h>
  18.  
  19. #include <graphics/gfxbase.h>
  20.  
  21. #include <utility/date.h>
  22.  
  23. #include <devices/conunit.h>
  24. #include <devices/serial.h>
  25. #include <devices/timer.h>
  26.  
  27. #include <hardware/cia.h>
  28.  
  29. #include <dos/dosextens.h>
  30. #include <dos/filehandler.h>
  31. #include <dos/dosasl.h>
  32.  
  33. #include <exec/memory.h>
  34.  
  35.     /* Correct a nasty bug in the prototypes. */
  36.  
  37. #define CheckIO foo21234
  38.  
  39. #include <clib/intuition_protos.h>
  40. #include <clib/gadtools_protos.h>
  41. #include <clib/graphics_protos.h>
  42. #include <clib/utility_protos.h>
  43. #include <clib/timer_protos.h>
  44. #include <clib/exec_protos.h>
  45. #include <clib/dos_protos.h>
  46. #include <clib/macros.h>
  47.  
  48.     /* Get the CheckIO prototype right. */
  49.  
  50. #undef CheckIO
  51.  
  52. struct IORequest *CheckIO(struct IORequest *);
  53.  
  54. #include "Rendezvous.h"
  55.  
  56. #include "hydracom.h"
  57.  
  58.     /* Serial buffer size. */
  59.  
  60. #define BUFFER_SIZE 8192
  61.  
  62.     /* A handy macro. */
  63.  
  64. #define ClrSignal(s)    SetSignal(0,s)
  65.  
  66.     /* Signal masks. */
  67.  
  68. #define SIG_SERREAD    (1UL << ReadPort -> mp_SigBit)
  69. #define SIG_SERWRITE    (1UL << WritePort -> mp_SigBit)
  70. #define SIG_CONREAD    (1UL << ConsoleReadPort -> mp_SigBit)
  71. #define SIG_TIMER    (1UL << TimePort -> mp_SigBit)
  72. #define SIG_WINDOW    (1UL << LocalWindow -> UserPort -> mp_SigBit)
  73.  
  74.     /* A serial buffer structure. */
  75.  
  76. struct SerialBuffer
  77. {
  78.     struct IOExtSer    *SerialRequest;
  79.     UBYTE        *SerialBuffer,
  80.             *SerialIndex,
  81.             *SerialTop;
  82.     LONG         SerialSize,
  83.              SerialFilled;
  84.     BOOL         IsClone,
  85.              IsBusy;
  86. };
  87.  
  88. STATIC struct RendezvousData        *RendezvousData;
  89. STATIC struct RendezvousSemaphore    *RendezvousSemaphore;
  90.  
  91.     /* Library bases. */
  92.  
  93. struct IntuitionBase    *IntuitionBase;
  94. struct GfxBase        *GfxBase;
  95. struct Library        *GadToolsBase,
  96.             *UtilityBase,
  97.             *TimerBase;
  98.  
  99.     /* Timer data. */
  100.  
  101. struct MsgPort        *TimePort;
  102. struct timerequest    *TimeRequest;
  103.  
  104.     /* Serial data. */
  105.  
  106. struct MsgPort        *ReadPort,
  107.             *WritePort;
  108.  
  109. struct SerialBuffer    *ThisBuffer,
  110.             *NextBuffer,
  111.             *ReadBuffer;
  112.  
  113.     /* Console data. */
  114.  
  115. struct Window        *FileWindow,
  116.             *RemoteWindow,
  117.             *LocalWindow,
  118.             *LogWindow;
  119.  
  120. struct MsgPort        *ConsoleWritePort,
  121.             *ConsoleReadPort;
  122. struct IOStdReq        *ConsoleReadRequest;
  123. UBYTE             ConsoleChar;
  124. BOOL             ConsoleReady = FALSE,
  125.              WindowReady = FALSE;
  126.  
  127. struct IOStdReq        *FileRequest,
  128.             *RemoteRequest,
  129.             *LocalRequest,
  130.             *LogRequest;
  131.  
  132.     /* DOS Data. */
  133.  
  134. struct Process        *ThisProcess;
  135. APTR             OldPtr;
  136.  
  137. struct AnchorPath    *Anchor;
  138. BOOL             AnchorUsed = FALSE;
  139.  
  140.     /* Screen data. */
  141.  
  142. struct Screen        *PublicScreen,
  143.             *Screen;
  144.  
  145.     /* Menu data. */
  146.  
  147. APTR             VisualInfo;
  148. struct Menu        *Menu;
  149.  
  150. struct NewMenu MenuTemplate[] =
  151. {
  152.     { NM_TITLE, "Project",             0 ,    0,    0,    (APTR)0},
  153.     {  NM_ITEM, "Toggle chat",        "C",    0,    0,    (APTR)Alt_C},
  154.     {  NM_ITEM, NM_BARLABEL,         0 ,    0,    0,    (APTR)0},
  155.     {  NM_ITEM, "Hang up",            "H",    0,    0,    (APTR)Alt_H},
  156.     {  NM_ITEM, "Toggle duplex",        "E",    0,    0,    (APTR)Alt_E},
  157.     {  NM_ITEM, "Toggle 7 bits/8 bits",    "B",    0,    0,    (APTR)Alt_B},
  158.     {  NM_ITEM, NM_BARLABEL,         0 ,    0,    0,    (APTR)0},
  159.     {  NM_ITEM, "Start upload",        "U",    0,    0,    (APTR)PgUp},
  160.     {  NM_ITEM, "Start download",        "D",    0,    0,    (APTR)PgDn},
  161.     {  NM_ITEM, NM_BARLABEL,         0 ,    0,    0,    (APTR)0},
  162.     {  NM_ITEM, "Exit HydraCom",        "Q",    0,    0,    (APTR)Alt_X},
  163.     { NM_END,   0,                 0 ,    0,    0,    (APTR)0}
  164. };
  165.  
  166.     /* OpenConsole():
  167.      *
  168.      *    Open a console window.
  169.      */
  170.  
  171. STATIC BOOL __inline
  172. OpenConsole(struct Screen *Screen,LONG Top,LONG Height,STRPTR Title,BOOL Resize,struct Window **WindowPtr,struct IOStdReq **ConsolePtr)
  173. {
  174.     struct Window *Window;
  175.  
  176.     if(Window = OpenWindowTags(NULL,
  177.         WA_Left,        0,
  178.         WA_Top,            Top,
  179.         WA_Width,        Screen -> Width,
  180.         WA_Height,        Height,
  181.         WA_Title,        Title,
  182.         WA_SimpleRefresh,    TRUE,
  183.         WA_DepthGadget,        TRUE,
  184.         WA_DragBar,        TRUE,
  185.         WA_SizeGadget,        Resize,
  186.         WA_SizeBRight,        TRUE,
  187.         WA_CustomScreen,    Screen,
  188.         WA_RMBTrap,        TRUE,
  189.         WA_NewLookMenus,    TRUE,
  190.     TAG_DONE))
  191.     {
  192.         struct IOStdReq *ConsoleRequest;
  193.  
  194.         if(Window -> RPort -> Font -> tf_Flags & FPF_PROPORTIONAL)
  195.             SetFont(Window -> RPort,GfxBase -> DefaultFont);
  196.  
  197.         if(ConsoleRequest = (struct IOStdReq *)CreateIORequest(ConsoleWritePort,sizeof(struct IOStdReq)))
  198.         {
  199.             ConsoleRequest -> io_Data = Window;
  200.  
  201.             if(!OpenDevice("console.device",CONU_CHARMAP,ConsoleRequest,CONFLAG_DEFAULT))
  202.             {
  203.                 WindowLimits(Window,Window -> BorderLeft + 10 * Window -> RPort -> Font -> tf_XSize * 10 + Window -> BorderRight,Window -> BorderTop + 2 * Window -> RPort -> Font -> tf_YSize + Window -> BorderBottom,Screen -> Width,Screen -> Height);
  204.  
  205.                     /* Turn off the cursor. */
  206.  
  207.                 ConPrintf(ConsoleRequest,"\033[0 p");
  208.  
  209.                 *WindowPtr    = Window;
  210.                 *ConsolePtr    = ConsoleRequest;
  211.  
  212.                 return(TRUE);
  213.             }
  214.  
  215.             DeleteIORequest(ConsoleRequest);
  216.         }
  217.  
  218.         CloseWindow(Window);
  219.     }
  220.  
  221.     return(FALSE);
  222. }
  223.  
  224.     /* CloseConsole():
  225.      *
  226.      *    Close a console window.
  227.      */
  228.  
  229. STATIC VOID __inline
  230. CloseConsole(struct Window **WindowPtr,struct IOStdReq **ConsolePtr)
  231. {
  232.     if(*ConsolePtr)
  233.     {
  234.         CloseDevice(*ConsolePtr);
  235.  
  236.         DeleteIORequest(*ConsolePtr);
  237.  
  238.         *ConsolePtr = NULL;
  239.     }
  240.  
  241.     if(*WindowPtr)
  242.     {
  243.         CloseWindow(*WindowPtr);
  244.  
  245.         *WindowPtr = NULL;
  246.     }
  247. }
  248.  
  249.     /* CloneSerialBuffer():
  250.      *
  251.      *    Clone a SerialBuffer structure.
  252.      */
  253.  
  254. STATIC struct SerialBuffer * __regargs
  255. CloneSerialBuffer(struct SerialBuffer *Source,struct MsgPort *MsgPort)
  256. {
  257.     struct SerialBuffer *Buffer;
  258.  
  259.     if(Buffer = (struct SerialBuffer *)AllocVec(sizeof(struct SerialBuffer) + Source -> SerialSize,MEMF_ANY | MEMF_PUBLIC))
  260.     {
  261.         Buffer -> SerialBuffer    = Buffer -> SerialIndex = (UBYTE *)(Buffer + 1);
  262.         Buffer -> SerialFilled    = 0;
  263.         Buffer -> SerialTop    = Buffer -> SerialBuffer + Source -> SerialSize;
  264.         Buffer -> SerialSize    = Source -> SerialSize;
  265.         Buffer -> IsClone    = TRUE;
  266.         Buffer -> IsBusy    = FALSE;
  267.  
  268.         if(Buffer -> SerialRequest = (struct IOExtSer *)AllocVec(sizeof(struct IOExtSer),MEMF_ANY | MEMF_PUBLIC))
  269.         {
  270.             CopyMem(Source -> SerialRequest,Buffer -> SerialRequest,sizeof(struct IOExtSer));
  271.  
  272.             Buffer -> SerialRequest -> IOSer . io_Message . mn_ReplyPort = MsgPort;
  273.  
  274.             return(Buffer);
  275.         }
  276.         else
  277.             cprint("Could not create serial request\n");
  278.  
  279.         FreeVec(Buffer);
  280.     }
  281.     else
  282.         cprint("Could not create serial buffer\n");
  283.  
  284.     return(NULL);
  285. }
  286.  
  287.     /* DeleteSerialBuffer():
  288.      *
  289.      *    Delete a SerialBuffer structure.
  290.      */
  291.  
  292. STATIC VOID __regargs
  293. DeleteSerialBuffer(struct SerialBuffer *Buffer)
  294. {
  295.     if(Buffer)
  296.     {
  297.         if(Buffer -> IsBusy)
  298.         {
  299.             if(!CheckIO(Buffer -> SerialRequest))
  300.                 AbortIO(Buffer -> SerialRequest);
  301.  
  302.             WaitIO(Buffer -> SerialRequest);
  303.         }
  304.  
  305.         if(Buffer -> IsClone)
  306.             FreeVec(Buffer -> SerialRequest);
  307.         else
  308.         {
  309.             CloseDevice(Buffer -> SerialRequest);
  310.  
  311.             DeleteIORequest(Buffer -> SerialRequest);
  312.         }
  313.  
  314.         FreeVec(Buffer);
  315.     }
  316. }
  317.  
  318.     /* CreateSerialBuffer():
  319.      *
  320.      *    Create a serial buffer structure.
  321.      */
  322.  
  323. STATIC struct SerialBuffer * __regargs
  324. CreateSerialBuffer(STRPTR Device,LONG Unit,LONG Size,struct MsgPort *MsgPort)
  325. {
  326.     struct SerialBuffer *Buffer;
  327.  
  328.     if(Buffer = (struct SerialBuffer *)AllocVec(sizeof(struct SerialBuffer) + Size,MEMF_ANY | MEMF_PUBLIC))
  329.     {
  330.         Buffer -> SerialBuffer    = Buffer -> SerialIndex = (UBYTE *)(Buffer + 1);
  331.         Buffer -> SerialFilled    = 0;
  332.         Buffer -> SerialTop    = Buffer -> SerialBuffer + Size;
  333.         Buffer -> SerialSize    = Size;
  334.         Buffer -> IsClone    = FALSE;
  335.         Buffer -> IsBusy    = FALSE;
  336.  
  337.         if(Buffer -> SerialRequest = (struct IOExtSer *)CreateIORequest(MsgPort,sizeof(struct IOExtSer)))
  338.         {
  339.             if(!OpenDevice(Device,Unit,Buffer -> SerialRequest,NULL))
  340.                 return(Buffer);
  341.             else
  342.             {
  343.                 cprint("Could not open \"%s\", unit %d\n",Device,Unit);
  344.  
  345.                 DeleteIORequest(Buffer -> SerialRequest);
  346.             }
  347.         }
  348.         else
  349.             cprint("Could not create serial request\n");
  350.  
  351.         FreeVec(Buffer);
  352.     }
  353.     else
  354.         cprint("Could not create serial buffer\n");
  355.  
  356.     return(NULL);
  357. }
  358.  
  359.     /* OpenAll():
  360.      *
  361.      *    Allocate all the resources required.
  362.      */
  363.  
  364. STATIC BOOL
  365. OpenAll(STRPTR Device,LONG Unit)
  366. {
  367.     LONG Top,Lines,BorderSize,FontSize,ExtraLines,RemainingLines,TotalHeight;
  368.     UWORD Pens = (UWORD)~0;
  369.  
  370.     if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",37)))
  371.     {
  372.         cprint("Could not open intuition.library v37\n");
  373.  
  374.         return(FALSE);
  375.     }
  376.  
  377.     if(!(Gfx